<?php
include 'portlet_header.php';
if (getParameter("operation")!=null) $operation=getParameter("operation");
if (getParameter("portlet")!=null) $portlet=getParameter("portlet");
if (getParameter("portletId")!=null) $portletId=getParameter("portletId");
if (getParameter("pageId")!=null) $pageId=getParameter("pageId");

if (getParameter("portalId")!=null) $portalId=getParameter("portalId");

// Include classes/* *  * require   $_SERVER['DOCUMENT_ROOT']."/php/tbs/tbs_class.php"; // Load the TinyButStrong template enginerequire   $_SERVER['DOCUMENT_ROOT']."/php/tbs/tbs_plugin_opentbs.php"; // Load the OpenTBS plugin * */
include_once( $_SERVER['DOCUMENT_ROOT'].'/php/tbs/tbs_class.php'); // Load the TinyButStrong template engine
include_once( $_SERVER['DOCUMENT_ROOT'].'/php/tbs/tbs_plugin_opentbs.php'); // Load the OpenTBS plugin


// Initalize the TBS instance
$TBS = new clsTinyButStrong; // new instance of TBS
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load the OpenTBS plugin


// ----------------------
// Debug mode of the demo
// ----------------------
if (isset($debug) && ($debug=='current')) $TBS->Plugin(OPENTBS_DEBUG_XML_CURRENT, true); // Display the intented XML of the current sub-file, and exit.
if (isset($debug) && ($debug=='info'))    $TBS->Plugin(OPENTBS_DEBUG_INFO, true); // Display information about the document, and exit.
if (isset($debug) && ($debug=='show'))    $TBS->Plugin(OPENTBS_DEBUG_XML_SHOW); // Tells TBS to display information when the document is merged. No exit.



if ($operation=="export")
{

	// ------------------------------
	// Prepare some data for the demo
	// ------------------------------
	
	// Retrieve the user name to display
	/* $docteur = $app_cabinet_docteur;
    $ville = $app_cabinet_ville;
    $adresse = $app_cabinet_adresse;
    $info = $app_cabinet_sousnom;
    $info2 = $app_cabinet_sousnom2;*/
	
    $task = "qqq";
    $duree = "2";
	$date = "29/09/2013";

	
	$data = array();
	 $data[] = array('libelle'=>'ok1'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok2'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok3'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok4'     , 'texte'=>'kokok');
	
	// -----------------
	// Load the template
	// -----------------

	
	$template = getFilesPath("templates").'/fsq.docx';
	$TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8); // Also merge some [onload] automatic fields (depends of the type of document).

	$TBS->PlugIn(OPENTBS_DELETE_COMMENTS);

	
	$TBS->MergeBlock('a', $data);
	// -----------------
	// Output the result
	// -----------------
	
	// Define the name of the output file
	$save_as = (isset($_POST['save_as']) && (trim($_POST['save_as'])!=='') && ($_SERVER['SERVER_NAME']=='localhost')) ? trim($_POST['save_as']) : '';
	$output_file_name = str_replace('.', '_'.date('Y-m-d').$save_as.'.', "FSQ").".docx";
	if ($save_as==='') {
		// Output the result as a downloadable file (only streaming, no data saved in the server)
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name); // Also merges all [onshow] automatic fields.
	} else {
		// Output the result as a file on the server
		$TBS->Show(OPENTBS_FILE+TBS_EXIT, $output_file_name); // Also merges all [onshow] automatic fields.
	}
	
	
}elseif($operation=="genererordonnance"){
	
	$soinId=getParameter("soinId");

	$SQL="SELECT 
cabinet_medicaments.texte,
cabinet_medicaments.libelle
FROM
cabinet_ordonnance_medicament
INNER JOIN cabinet_medicaments ON cabinet_ordonnance_medicament.medicamentId = cabinet_medicaments.id
WHERE
cabinet_ordonnance_medicament.portalId = $portalId AND
cabinet_ordonnance_medicament.soinId = $soinId";
	
//	echo $SQL;
	
	$rs=ew_LoadRecordset($SQL);
	// A recordset for merging tables
	$data = array();
	while (!$rs->EOF) {
		

		 $data[] = array('libelle'=>$rs->fields['libelle']     , 'texte'=>$rs->fields['texte']);
		$rs->MoveNext();
	}

	
	//print_r($data);
	
	/*//$data = array();
	$data[] = array('libelle'=>'ok1'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok2'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok3'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok4'     , 'texte'=>'kokok');*/

	// ------------------------------
	// Prepare some data for the demo
	// ------------------------------
	
	// Retrieve the user name to display
    $docteur = $app_cabinet_docteur;
    $ville = $app_cabinet_ville;
    $adresse = $app_cabinet_adresse;
    $info = $app_cabinet_sousnom;
    $info2 = $app_cabinet_sousnom2;
	$date = "29/09/2013";
	$patient = "qqq";

	
	// -----------------
	// Load the template
	// -----------------
	
	$template = getFilesPath("templates").'/ordonnance.docx';
	$TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8); // Also merge some [onload] automatic fields (depends of the type of document).
	
	
	
	/*$data = array();
	$data[] = array('rank'=> 'A', 'firstname'=>'Sandra' , 'name'=>'Hill'      , 'number'=>'1523d', 'score'=>200, 'email_1'=>'sh@tbs.com',  'email_2'=>'sandra@tbs.com',  'email_3'=>'s.hill@tbs.com');
	$data[] = array('rank'=> 'A', 'firstname'=>'Roger'  , 'name'=>'Smith'     , 'number'=>'1234f', 'score'=>800, 'email_1'=>'rs@tbs.com',  'email_2'=>'robert@tbs.com',  'email_3'=>'r.smith@tbs.com' );
	$data[] = array('rank'=> 'B', 'firstname'=>'William', 'name'=>'Mac Dowell', 'number'=>'5491y', 'score'=>130, 'email_1'=>'wmc@tbs.com', 'email_2'=>'william@tbs.com', 'email_3'=>'w.m.dowell@tbs.com' );
	
	

*/
	
	$TBS->MergeBlock('a,b', $data);
	
	
	

	$TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
	
	// -----------------
	// Output the result
	// -----------------
	
	// Define the name of the output file
	$save_as = (isset($_POST['save_as']) && (trim($_POST['save_as'])!=='') && ($_SERVER['SERVER_NAME']=='localhost')) ? trim($_POST['save_as']) : '';
	$output_file_name = str_replace('.', '_'.date('Y-m-d').$save_as.'.', "ordonnance").".docx";
	if ($save_as==='') {
		// Output the result as a downloadable file (only streaming, no data saved in the server)
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name); // Also merges all [onshow] automatic fields.
	} else {
		// Output the result as a file on the server
		$TBS->Show(OPENTBS_FILE+TBS_EXIT, $output_file_name); // Also merges all [onshow] automatic fields.
	}
	
	
}elseif($operation=="genererdevis"){
	
	$devisId=getParameter("devisId");

	$SQL="SELECT
cabinet_devis_soin.dent,
cabinet_soinnature.nom,
cabinet_devis_soin.montant,
cabinet_devis_soin.texte
FROM
cabinet_devis_soin
INNER JOIN cabinet_soinnature ON cabinet_devis_soin.naturedesoinId = cabinet_soinnature.id
WHERE
cabinet_devis_soin.devisId = $devisId AND
cabinet_devis_soin.portalId = $portalId
	";
	
	//echo $SQL;
	
	$rs=ew_LoadRecordset($SQL);
	// A recordset for merging tables
	$data = array();
	while (!$rs->EOF) {
		
	//	echo $rs->fields['libelle'];

array_push($data,array('soin'=>$rs->fields['nom']     , 'texte'=>$rs->fields['texte'], 'dent'=>$rs->fields['dent'], 'montant'=>$rs->fields['montant']));
		$rs->MoveNext();
	}

	
	//print_r($data);
	/*
	//$data = array();
	$data[] = array('libelle'=>'ok'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok'     , 'texte'=>'kokok');
	$data[] =array('libelle'=>'ok'     , 'texte'=>'kokok');
*/
	// ------------------------------
	// Prepare some data for the demo
	// ------------------------------
	
	// Retrieve the user name to display
	$docteur = $app_cabinet_docteur;
	$cabinet = "fff";
	$patient = "qqq";
	
	// -----------------
	// Load the template
	// -----------------
	
	$template = getFilesPath("templates").'/devis.docx';
	$TBS->LoadTemplate($template); // Also merge some [onload] automatic fields (depends of the type of document).
	
	
	
	/*$data = array();
	$data[] = array('rank'=> 'A', 'firstname'=>'Sandra' , 'name'=>'Hill'      , 'number'=>'1523d', 'score'=>200, 'email_1'=>'sh@tbs.com',  'email_2'=>'sandra@tbs.com',  'email_3'=>'s.hill@tbs.com');
	$data[] = array('rank'=> 'A', 'firstname'=>'Roger'  , 'name'=>'Smith'     , 'number'=>'1234f', 'score'=>800, 'email_1'=>'rs@tbs.com',  'email_2'=>'robert@tbs.com',  'email_3'=>'r.smith@tbs.com' );
	$data[] = array('rank'=> 'B', 'firstname'=>'William', 'name'=>'Mac Dowell', 'number'=>'5491y', 'score'=>130, 'email_1'=>'wmc@tbs.com', 'email_2'=>'william@tbs.com', 'email_3'=>'w.m.dowell@tbs.com' );
	
	

*/
	
	$TBS->MergeBlock('a,b', $data);
	
	
	

	$TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
	
	// -----------------
	// Output the result
	// -----------------
	
	// Define the name of the output file
	$save_as = (isset($_POST['save_as']) && (trim($_POST['save_as'])!=='') && ($_SERVER['SERVER_NAME']=='localhost')) ? trim($_POST['save_as']) : '';
	$output_file_name = str_replace('.', '_'.date('Y-m-d').$save_as.'.', "devis").".docx";
	if ($save_as==='') {
		// Output the result as a downloadable file (only streaming, no data saved in the server)
		$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name); // Also merges all [onshow] automatic fields.
	} else {
		// Output the result as a file on the server
		$TBS->Show(OPENTBS_FILE+TBS_EXIT, $output_file_name); // Also merges all [onshow] automatic fields.
	}
	
	
}
include 'portlet_footer.php';
?>